package com.qingfeng.client.api;

import com.qingfeng.client.bean.Grand;
import com.qingfeng.client.dao.GrandMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 品牌
 * @Author ff
 * @Data 2017/10/25 15:33
 */
@Api(tags = "品牌信息")
@RestController
@RequestMapping(value = "/api/grand")
public class GrandApi extends BaseV1Api {

    @Resource
    private GrandMapper grandDao;

    /**
     * 获取所有品牌
     * @return
     */
    @ApiOperation("获取商品所有品牌")
    @GetMapping(value = "/getAll")
    public Object getAll(){
        List<Grand> list = grandDao.queryAll();
        if (list==null){
            return error(null);
        }else{
            return success(list);
        }
    }

    @ApiOperation("获取子级品牌")
    @GetMapping(value = "/getByType/{typeId}")
    public Object getByType(@PathVariable(value = "typeId") Long typeId){
        List<Map<String,Object>> list = grandDao.getByType(typeId);
        if (list==null){
            return error(null);
        }else{
            return success(list);
        }
    }
    @ApiOperation("新增品牌")
    @PostMapping(value = "/add")
    public Object add(@RequestBody Grand grandBean){
        if (grandDao.insert(grandBean)>0){
            return success(null);
        }else {
            String msg = "品牌已存在";
            return error(msg);
        }
    }

    @ApiOperation("更新品牌")
    @PostMapping(value = "/update")
    public Object update(@RequestBody Grand grandBean){
        grandDao.updateById(grandBean);
        return success(null);
    }
}
